//
//  ViewController.m
//  FFUIFactoryDemo
//
//  Created by 刘彬 on 2016/10/18.
//  Copyright © 2016年 com.wanda.uifactory.demo. All rights reserved.
//

#import "FFUIFactoryViewController.h"
#import "UIViewSampleViewController.h"
#import "UILabelSampleViewController.h"
#import "UIButtonSampleViewController.h"
#import "UIImageViewViewController.h"
#import "UITextFieldSampleViewController.h"
#import "UIColorSampleViewController.h"

@interface FFUIFactoryViewController ()

@property (nonatomic, strong) UIButton *viewSampleBtn;
@property (nonatomic, strong) UIButton *labelSampleBtn;
@property (nonatomic, strong) UIButton *buttonSampleBtn;
@property (nonatomic, strong) UIButton *imageViewSampleBtn;
@property (nonatomic, strong) UIButton *textFieldBtn;
@property (nonatomic, strong) UIButton *colorBtn;

@end

@implementation FFUIFactoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"FFUIFactory";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewSampleBtn];
    [self.view addSubview:self.labelSampleBtn];
    [self.view addSubview:self.buttonSampleBtn];
    [self.view addSubview:self.imageViewSampleBtn];
    [self.view addSubview:self.textFieldBtn];
    [self.view addSubview:self.colorBtn];
    
    NSArray *btnArray = @[self.viewSampleBtn,self.labelSampleBtn,self.buttonSampleBtn,self.imageViewSampleBtn,self.textFieldBtn,self.colorBtn];
    CGFloat margin = (self.view.ff_height - 20 - 44 -50*btnArray.count)/(btnArray.count+1);
    for (int i = 0; i < btnArray.count; i++) {
        UIButton *currentBtn = [btnArray safeObjectAtIndex:i];
        UIButton *previousBtn = [btnArray safeObjectAtIndex:i-1];
        [currentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(previousBtn ? previousBtn.mas_bottom : self.view).offset(previousBtn ? margin : margin+20+44);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(200, 50));
        }];
    }
}

- (void)buttonAction:(UIButton *)button{
    UIViewController *sampleViewController = nil;
    switch (button.tag) {
        case 1001:{
            sampleViewController = [[UIViewSampleViewController alloc] init];
            break;
        }
        case 1002:{
            sampleViewController = [[UILabelSampleViewController alloc] init];
            break;
        }
        case 1003:{
            sampleViewController = [[UIButtonSampleViewController alloc] init];
            break;
        }
        case 1004:{
            sampleViewController = [[UIImageViewViewController alloc] init];
            break;
        }
        case 1005:{
            sampleViewController = [[UITextFieldSampleViewController alloc] init];
            break;
        }
        case 1006:{
            sampleViewController = [[UIColorSampleViewController alloc] init];
            break;
        }
        default:
            break;
    }
    sampleViewController.title = button.ff_normalTitle;
    [self.navigationController pushViewController:sampleViewController animated:YES];
}

- (UIButton *)viewSampleBtn{
    if (_viewSampleBtn == nil) {
        _viewSampleBtn = [self generateButton];
        _viewSampleBtn.tag = 1001;
        _viewSampleBtn.ff_normalTitle = @"UIView";
        [_viewSampleBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _viewSampleBtn;
}

- (UIButton *)labelSampleBtn{
    if (_labelSampleBtn == nil) {
        _labelSampleBtn = [self generateButton];
        _labelSampleBtn.tag = 1002;
        _labelSampleBtn.ff_normalTitle = @"UILabel";
        [_labelSampleBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _labelSampleBtn;
}

- (UIButton *)buttonSampleBtn{
    if (_buttonSampleBtn == nil) {
        _buttonSampleBtn = [self generateButton];
        _buttonSampleBtn.tag = 1003;
        _buttonSampleBtn.ff_normalTitle = @"UIButton";
        [_buttonSampleBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _buttonSampleBtn;
}

- (UIButton *)imageViewSampleBtn{
    if (_imageViewSampleBtn == nil) {
        _imageViewSampleBtn = [self generateButton];
        _imageViewSampleBtn.tag = 1004;
        _imageViewSampleBtn.ff_normalTitle = @"UIImageView";
        [_imageViewSampleBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _imageViewSampleBtn;
}

- (UIButton *)textFieldBtn{
    if (_textFieldBtn == nil) {
        _textFieldBtn = [self generateButton];
        _textFieldBtn.tag = 1005;
        _textFieldBtn.ff_normalTitle = @"UITextField";
        [_textFieldBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _textFieldBtn;
}

- (UIButton *)colorBtn{
    if (_colorBtn == nil) {
        _colorBtn = [self generateButton];
        _colorBtn.tag = 1006;
        _colorBtn.ff_normalTitle = @"UIColor";
        [_colorBtn ff_addTarget:self touchAction:@selector(buttonAction:)];
    }
    return _colorBtn;
}

- (UIButton *)generateButton{
    return [UIButton ff_buttonWithSize:CGSizeMake(200, 50) cornerRadius:5 font:[UIFont systemFontOfSize:15] normalColor:[UIColor whiteColor] selectedColor:[UIColor blackColor] disabledColor:nil normalBgColor:[UIColor blackColor] selectedBgColor:[UIColor whiteColor] disabledBgColor:nil];
}
@end
